# -*- coding: utf-8 -*-
"""
2024-02-12 
main
"""
"""


from PySide6.QtWidgets import QApplication, QWidget

app = QApplication([])

# 创建一个QWidget实例
widget = QWidget()

# 设置窗口标题
widget.setWindowTitle('My First QWidget')

# 设置窗口大小
widget.setGeometry(100, 100, 300, 200)

# 显示窗口
widget.show()

# 运行应用程序的主事件循环
"""
from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget,QDialog
from PySide6.QtCore import Qt
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

## from top.wardyan.core.utils import winformutils
from top.wardyan.core.sqlite import sqliteconfigs
from top.wardyan.core.vo.vos import TableCfg, TableColCfg, Record,WinCfg





class WMainWindow(QMainWindow):
    ## 窗体名
    def __init__(self, name : str):
        QMainWindow.__init__(self)

        
        self.setWindowFlags(self.windowFlags() | Qt.WindowMinMaxButtonsHint)
        
        self.name = name
        ## 读取配置
        cfgs : WinCfg = sqliteconfigs.readConfig(name)
        if cfgs.name == None or cfgs.name == "" or cfgs.name != self.name :
            cfgs.name = self.name
            sqliteconfigs.saveConfigs(cfgs)
        cfgs.name = self.name
        self.title = cfgs.title
        self.fullscreen = cfgs.isFullScreen
        self.width = cfgs.width
        self.height = cfgs.height
        self.x = cfgs.x
        self.y = cfgs.y
        self.position = cfgs.position

        ## 设置
        self.setWindowTitle(self.title)
        if self.position == 0 :
            ## 如果居中窗口
            screen = QGuiApplication.primaryScreen().availableGeometry()
            self.x = (screen.width() - self.width) / 2
            self.y = (screen.height() - self.height) / 2

        self.setGeometry(self.x, self.y, self.width, self.height)
        ## 下面这个不能要, 会导致最大化按钮没了
        ## geometry = self.screen().availableGeometry()
        if self.fullscreen :
            self.showMaximized()
        

    def closeEvent(self, event: QCloseEvent) -> None:
        """关闭窗体时记录窗体信息
        """
        rs = super().closeEvent(event) 
        ## 
        g = self.geometry()
        x = g.x()
        y = g.y()
        width = g.width()
        height = g.height()
        fullscreen = self.isMaximized()
        cfg = sqliteconfigs.readConfig(self.name)
        if not fullscreen :
            ## 最大化不改变原来所在的位置和大小
            cfg.x = x
            cfg.y = y
            cfg.width = width
            cfg.height = height
        cfg.isFullScreen = fullscreen
        sqliteconfigs.saveConfigs(cfg)
        



class WQdialog(QDialog):
    ## 窗体名
    def __init__(self, name : str):
        QDialog.__init__(self)
        self.name = name

        
        self.setWindowFlags(self.windowFlags() | Qt.WindowMinMaxButtonsHint)

        ## 读取配置
        cfgs : WinCfg = sqliteconfigs.readConfig(name)
        if cfgs.name == None or cfgs.name == "" or cfgs.name != self.name :
            cfgs.name = self.name
            sqliteconfigs.saveConfigs(cfgs)
            
        self.title = cfgs.title
        self.fullscreen = cfgs.isFullScreen
        self.width = cfgs.width
        self.height = cfgs.height
        self.x = cfgs.x
        self.y = cfgs.y
        if self.x < 0 :
            self.x = 0
        if self.y < 0 :
            self.y = 0
        self.position = cfgs.position

        ## 设置
        self.setWindowTitle(self.title)
        if self.position == 0 :
            ## 如果居中窗口
            screen = QGuiApplication.primaryScreen().availableGeometry()
            ## size = self.geometry()
            self.x = (screen.width() - self.width) / 2
            self.y = (screen.height() - self.height) / 2

        self.setGeometry(self.x, self.y, self.width, self.height)
        ## 下面这个不能要, 会导致最大化按钮没了
        if self.fullscreen :
            self.showMaximized()
        

    def closeEvent(self, event: QCloseEvent) -> None:
        """关闭窗体时记录窗体信息
        """
        rs = super().closeEvent(event) 

        ## 
        g = self.geometry()
        ## QGuiApplication.screens()
        x = g.x()
        y = g.y()
        width = g.width()
        height = g.height()
        fullscreen = self.isMaximized()
        cfg = sqliteconfigs.readConfig(self.name)
        if not fullscreen :
            ## 最大化不改变原来所在的位置和大小
            cfg.x = x
            cfg.y = y
            cfg.width = width
            cfg.height = height
        cfg.isFullScreen = fullscreen
        sqliteconfigs.saveConfigs(cfg)
        


if __name__ == "__main__" :   ## or 1 == 1
    app = QApplication([])
    w = WMainWindow("testing")
    w.show()
    app.exec()


## print(__file__)